<script>
	if ('serviceWorker' in navigator) {
		// Delay registration until after the page has loaded, to ensure that our
		// precaching requests don't degrade the first visit experience.
		// See https://developers.google.com/web/fundamentals/instant-and-offline/service-worker/registration
		window.addEventListener('load', function () {
			navigator.serviceWorker
				.register('service-worker.js')
				.then(function (reg) {
					// Check for updates
					reg.onupdatefound = function () {
						const installingWorker = reg.installing;

						installingWorker.onstatechange = function () {
							/* eslint-disable default-case */
							switch (installingWorker.state) {
								case 'installed':
									if (navigator.serviceWorker.controller) {
										// New content is available, refresh the page
										window.location.reload();
									} else {
										console.log('Content is now available offline!');
									}
									break;

								case 'redundant':
									console.error(
										'The installing service worker became redundant.'
									);
									break;
							}
						};
					};
				})
				.catch(function (e) {
					console.error('Error during service worker registration:', e);
				});
		});
	}
</script>

<script>
	window.addEventListener('message', e => {
		// console.log(88, e.data);
		if (navigator.serviceWorker.controller) {
			navigator.serviceWorker.controller.postMessage(e.data);
		} else {
			console.warn('Service worker not ready, reloading...');
			window.location.reload();
		}
	});
</script>
